﻿using Freshbox2._0.BLL;
using Freshbox2._0.MODEL;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Web;
using System.Web.Mvc;

namespace Freshbox2._0.UI.Controllers
{
    public class LossChartController : BaseController<stockloss>
    {
        //
        // GET: /LossChart/


        public override BLL.BaseBll<stockloss> bll
        {
            get
            {
                return new StockLossBll();
            }
    
        }

        public class LossModel
        {
            public int? lossNumber;
            public string waresCode;
            public string waresName;
            public decimal? waresPrice;
            public decimal? waresAllPrice;

        }
        public class ChartModel
        {
            /// <summary>
            /// 横坐标日期ss
            /// </summary>
            public string Date { get; set; }
            /// <summary>
            /// 亏损钱数
            /// </summary>
            public string CountMoney { get; set; }
        }

        public int lossNum = 0;//数量

        public decimal lossMoney = 0.00m;//亏损金额 

        public ActionResult GetAll(string date1, string date2, string storeid, stockloss model)
        {
            List<LossModel> LossList = new List<LossModel>();
                      
            Expression<Func<stockloss, bool>> condition = null;

            //默认情况   默认查询近一个月的 四周的 所有记录  
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && string.IsNullOrEmpty(storeid))
            {
                for (var i = 0; i <=3; i++)
                {   
                    //>=3.27  <=4.3
                    DateTime today = DateTime.Now.AddDays(-(i*7)).AddDays(1).Date;
                    DateTime before = today.AddDays(-7).Date;
                    condition = x => x.operateTime <= today && x.operateTime >= before;
                    var listOrder = bll.Search(condition).ToList();
                    var list = listOrder.GroupBy(x=>x.waresCode).Select(x => new { lossNumber = x.Sum(y => y.lossNumber), x.FirstOrDefault().waresCode, x.FirstOrDefault().waresName, x.FirstOrDefault().waresPrice }).ToList();
                    //遍历
                    foreach (var item in list)
                    {
                        LossModel modelLoss = new LossModel();
                        modelLoss.lossNumber = item.lossNumber;
                        modelLoss.waresPrice = item.waresPrice;
                        modelLoss.waresAllPrice = modelLoss.lossNumber * modelLoss.waresPrice;
                        LossList.Add(modelLoss);
                        lossNum = lossNum + Convert.ToInt32(modelLoss.lossNumber);
                        lossMoney = lossMoney + Convert.ToDecimal(modelLoss.waresAllPrice);
                    }
                    ChartModel modelChart = new ChartModel();
                    modelChart.Date = today.ToString("yyyy/MM/dd");
                    modelChart.CountMoney = lossMoney.ToString();      

                }

            }
            //只按照店铺查询 默认是查询一周的店铺记录  
            if (string.IsNullOrEmpty(date1) && string.IsNullOrEmpty(date2) && storeid != "" && storeid != null)
            {
                int shopid = Convert.ToInt32(storeid);
                DateTime todayNow = DateTime.Now;
                DateTime midnight = DateTime.Now.AddDays(-6);
                midnight = midnight.Date;
                condition = x => x.operateTime <= todayNow && x.operateTime >= midnight && x.storeid == shopid;
            }
            //按照时间和店铺查询   
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && storeid != "")
            {
                int shopid = Convert.ToInt32(storeid);
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);

                condition = x => x.operateTime >= datetime1 && x.operateTime <= datetime2 && x.storeid == shopid;
            }
            //只按照时间查询  所有店铺的
            if (date1 != null && date1 != "" && date2 != null && date2 != "" && string.IsNullOrEmpty(storeid))
            {
                DateTime datetime1 = DateTime.Parse(date1);
                DateTime datetime2 = DateTime.Parse(date2).AddDays(1);
                condition = x => x.operateTime >= datetime1 && x.operateTime <= datetime2;
            }
            ////  是否有 商品名称
            ////1.
            //    var listOrder = bll.Search(condition).ToList();

            //    var list = listOrder.GroupBy(x=>x.waresCode).Select(x => new { lossNumber = x.Sum(y => y.lossNumber), x.FirstOrDefault().waresCode, x.FirstOrDefault().waresName, x.FirstOrDefault().waresPrice }).ToList();
            //    foreach (var item in list)
            //    {
            //        LossModel modelLoss = new LossModel();
            //        modelLoss.lossNumber = item.lossNumber;
            //        modelLoss.waresPrice = item.waresPrice;
            //        modelLoss.waresAllPrice = modelLoss.lossNumber * modelLoss.waresPrice;
            //        LossList.Add(modelLoss);
            //        lossNum = lossNum + Convert.ToInt32(modelLoss.lossNumber);
            //        lossMoney = lossMoney + Convert.ToDecimal(modelLoss.waresAllPrice);
            //    }
            return Json(new { rows = LossList, lossNum = lossNum, lossMoney = lossMoney }, JsonRequestBehavior.AllowGet);
        }
    }
}
